﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using Rectangle = System.Windows.Shapes.Rectangle;

namespace AutoEditUI
{
    internal class UIInit
    {
        private static void UIAdd(UIItem mItemIcon)
        {
            MainWindow.gItems.Add(mItemIcon);
            if (!string.IsNullOrEmpty(mItemIcon.LeftItemTag))
            {
                if (!MainWindow.xyRelate.Contains(mItemIcon.LeftItemTag))
                    MainWindow.xyRelate.Add(mItemIcon.LeftItemTag);
            }
            if (!string.IsNullOrEmpty(mItemIcon.UpItemTag))
            {
                if (!MainWindow.xyRelate.Contains(mItemIcon.UpItemTag))
                    MainWindow.xyRelate.Add(mItemIcon.UpItemTag);
            }
        }

        public static double GetY(MainWindow mainWindow, UIItem mUIItem)
        {
            if (!string.IsNullOrEmpty(mUIItem.UpItemTag))
            {
                UIItem xxx = mainWindow.GeItemByTag(mUIItem.UpItemTag);
                double ddd = Convert.ToDouble(mUIItem.UpValue) + GetY(mainWindow, xxx) + Convert.ToDouble(xxx.H);
                return ddd;
            }
            else
            {
                return mUIItem.Y;
            }
        }

        public static double GetX(MainWindow mainWindow, UIItem mUIItem)
        {
            if (!string.IsNullOrEmpty(mUIItem.LeftItemTag)) {
                UIItem xxx = mainWindow.GeItemByTag(mUIItem.LeftItemTag);
                double ddd = Convert.ToDouble(mUIItem.LeftValue) + GetX(mainWindow, xxx) + Convert.ToDouble(xxx.W);
                return ddd;
            }
            else
            {
                return mUIItem.X;
            }
        }
        internal static Image AddIconInit(MainWindow mainWindow, Canvas workCanvas, ItemIcon mItemIcon)
        {
            UIAdd(mItemIcon);

            Image x = new Image();
            x.Name = mItemIcon.ItemName;
            x.Tag = mItemIcon.Tag;
            x.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/Resources/bitbug_favicon128.ico", UriKind.RelativeOrAbsolute));
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Width = Convert.ToDouble(mItemIcon.W);
            x.Height = Convert.ToDouble(mItemIcon.H);
            Canvas.SetTop(x, GetY(mainWindow, mItemIcon));
            Canvas.SetLeft(x, GetX(mainWindow, mItemIcon));
            workCanvas.Children.Add(x);
            return x;
        }

        internal static Canvas AddCanvasInit(MainWindow mainWindow, Canvas workCanvas, ItemCanvas mItemCanvas)
        {
            UIAdd(mItemCanvas);
            Canvas canvasbase = new Canvas();
            canvasbase.Name = mItemCanvas.ItemName;
            canvasbase.Tag = mItemCanvas.Tag;
            canvasbase.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            canvasbase.Width = Convert.ToDouble(mItemCanvas.W);
            canvasbase.Height = Convert.ToDouble(mItemCanvas.H);
            Canvas.SetTop(canvasbase, GetY(mainWindow, mItemCanvas));
            Canvas.SetLeft(canvasbase, GetX(mainWindow, mItemCanvas));

            using (StreamReader sRead = new StreamReader(MainWindow.gNowPath + @"\CElogo.xaml")) { 
                string nowPC = sRead.ReadToEnd();
                mItemCanvas.ItemBin = nowPC;
                sRead.Close();
            }
            workCanvas.Children.Add(canvasbase);
            XmlTextReader xmlreader = new XmlTextReader(MainWindow.gNowPath+@"\CElogo.xaml");
           
            Canvas x = System.Windows.Markup.XamlReader.Load(xmlreader) as Canvas;
            Canvas.SetTop(x, 0);
            Canvas.SetLeft(x, 0);
            canvasbase.Children.Add(x);
            return canvasbase;
        }
        internal static Image AddIcon(MainWindow mainWindow, Canvas workCanvas, ItemIcon mItemIcon)
        {
            UIAdd(mItemIcon);
            Image x = new Image();
            x.Name = mItemIcon.ItemName;
            x.Tag = mItemIcon.Tag;
            byte[] xxxx = System.Convert.FromBase64String(mItemIcon.ItemBin);
            x.Source = PicConverter.ByteArray2BitmapSource(xxxx);
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Width = Convert.ToDouble(mItemIcon.W);
            x.Height = Convert.ToDouble(mItemIcon.H);
            Canvas.SetTop(x, GetY(mainWindow, mItemIcon));
            Canvas.SetLeft(x, GetX(mainWindow, mItemIcon));
            workCanvas.Children.Add(x);
            return x;
        }

        internal static TextBlock AddText(MainWindow mainWindow, Canvas workCanvas, ItemText mItemText)
        {
            UIAdd(mItemText);
            TextBlock x = new TextBlock();
            x.Name = mItemText.ItemName;
            x.Tag = mItemText.Tag;
            BrushConverter brushConverter = new BrushConverter();
            x.Background = (Brush)brushConverter.ConvertFromString(mItemText.Background); 
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Text = mItemText.Text+ mItemText.Text_;
            x.FontSize = mItemText.FontSize;
            x.Width = mItemText.W;
            x.Height = mItemText.H;
            x.FontFamily =new FontFamily(mItemText.FontFamily);
            x.FontWeight =  System.Windows.FontWeight.FromOpenTypeWeight(mItemText.FontWeight);
            if (mItemText.TextBackBlack==null  ||(bool)mItemText.TextBackBlack==false)
            {
                x.Foreground = Brushes.Black;
                x.Background = Brushes.White;
            }
            else
            {
                x.Foreground = Brushes.White;
                x.Background = Brushes.Black;
            }


            switch (mItemText.TextMiddle)
            {
                case "Left":
                    x.TextAlignment = TextAlignment.Left;
                    break;
                case "Middle":
                    x.TextAlignment = TextAlignment.Center;
                    break;
                case "Right":
                    x.TextAlignment = TextAlignment.Right;
                    break;
            }

            Canvas.SetTop(x, GetY(mainWindow, mItemText));
            Canvas.SetLeft(x, GetX(mainWindow, mItemText));
            workCanvas.Children.Add(x);
            return x;
        }

        internal static void AddCanvas(MainWindow mainWindow, Canvas workCanvas, ItemCanvas mItemCanvas)
        {
            UIAdd(mItemCanvas);
            Canvas canvasbase = new Canvas();
            canvasbase.Name = mItemCanvas.ItemName;
            canvasbase.Tag = mItemCanvas.Tag;
            canvasbase.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            canvasbase.Width = Convert.ToDouble(mItemCanvas.W);
            canvasbase.Height = Convert.ToDouble(mItemCanvas.H);
            Canvas.SetTop(canvasbase, GetY(mainWindow, mItemCanvas));
            Canvas.SetLeft(canvasbase, GetX(mainWindow, mItemCanvas));
            workCanvas.Children.Add(canvasbase);
            StringReader stringReader = new StringReader(mItemCanvas.ItemBin);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            Canvas x = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader);
            Canvas.SetTop(x, 0);
            Canvas.SetLeft(x, 0);
            canvasbase.Children.Add(x);
        }

        internal static Image AddQR(MainWindow mainWindow, Canvas workCanvas, ItemQR mItemQR)
        {
            UIAdd(mItemQR);
            Image x = new Image();
            x.Name = mItemQR.ItemName;
            x.Tag = mItemQR.Tag;
            System.Windows.Media.DrawingImage QRDrawingImage = Utils.WorkForSVG(mItemQR.ItemValue, 50);
            x.Source = Utils.WorkForSVG(mItemQR.ItemValue, 50);
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Width = Convert.ToDouble(mItemQR.W);
            x.Height = Convert.ToDouble(mItemQR.H);
            Canvas.SetTop(x, GetY(mainWindow, mItemQR));
            Canvas.SetLeft(x, GetX(mainWindow, mItemQR));
            workCanvas.Children.Add(x);
            return x;
        }



        internal static Image AddBarcode(MainWindow mainWindow, Canvas workCanvas, ItemBarcode mItemBarcode)
        {
            UIAdd(mItemBarcode);
            Image x = new Image();
            x.Name = mItemBarcode.ItemName;
            x.Tag = mItemBarcode.Tag;
            Code128 mBarcodeMK = new Code128();
            x.Source = mBarcodeMK.GetCodeImageMK(mItemBarcode.ItemValue, mItemBarcode.W, mItemBarcode.H);
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Width = Convert.ToDouble(mItemBarcode.W);
            x.Height = Convert.ToDouble(mItemBarcode.H);
            Canvas.SetTop(x, GetY(mainWindow, mItemBarcode));
            Canvas.SetLeft(x, GetX(mainWindow, mItemBarcode));
            workCanvas.Children.Add(x);
            return x;
        }
      
        internal static Rectangle AddRect(MainWindow mainWindow, Canvas workCanvas, ItemRect mItemRect)
        {
            UIAdd(mItemRect);
            Rectangle x = new Rectangle();
            x.Name = mItemRect.ItemName;
            x.Tag = mItemRect.Tag;
            x.MouseLeftButtonDown += new MouseButtonEventHandler(mainWindow.canvas2_MouseDown);
            x.Width = Convert.ToDouble(mItemRect.W);
            x.Height = Convert.ToDouble(mItemRect.H);
            x.Fill = Brushes.Black;
            x.Stroke = Brushes.Black;
            Canvas.SetTop(x, GetY(mainWindow, mItemRect));
            Canvas.SetLeft(x, GetX(mainWindow, mItemRect));
            workCanvas.Children.Add(x);
            return x;
        }
    }
}